package com.ruoyi.lol.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.lol.domain.LolHero;
import com.ruoyi.lol.mapper.LolHeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lol.mapper.LolGameRecordMapper;
import com.ruoyi.lol.domain.LolGameRecord;
import com.ruoyi.lol.service.ILolGameRecordService;

/**
 * 游戏对局记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-20
 */
@Service
public class LolGameRecordServiceImpl implements ILolGameRecordService {
    @Autowired
    private LolGameRecordMapper lolGameRecordMapper;

    @Autowired
    private LolHeroMapper lolHeroMapper;

    /**
     * 查询游戏对局记录
     *
     * @param id 游戏对局记录主键
     * @return 游戏对局记录
     */
    @Override
    public LolGameRecord selectLolGameRecordById(Long id) {
        return lolGameRecordMapper.selectLolGameRecordById(id);
    }

    /**
     * 查询游戏对局记录列表
     *
     * @param lolGameRecord 游戏对局记录
     * @return 游戏对局记录
     */
    @Override
    public List<LolGameRecord> selectLolGameRecordList(LolGameRecord lolGameRecord) {
        List<LolGameRecord> recordList = lolGameRecordMapper.selectLolGameRecordList(lolGameRecord);
        List<LolHero> heroList = lolHeroMapper.selectLolHeroList(new LolHero());
        Map<Long, String> heroMap = heroList.stream().collect(Collectors.toMap(LolHero::getId, LolHero::getName));
        recordList.forEach(record -> {
            record.setTop(heroMap.get(record.getTopId()));
            record.setJungle(heroMap.get(record.getJungleId()));
            record.setMid(heroMap.get(record.getMidId()));
            record.setAd(heroMap.get(record.getAdId()));
            record.setSup(heroMap.get(record.getSupId()));
            record.setTop2(heroMap.get(record.getTop2Id()));
            record.setJungle2(heroMap.get(record.getJungle2Id()));
            record.setMid2(heroMap.get(record.getMid2Id()));
            record.setAd2(heroMap.get(record.getAd2Id()));
            record.setSup2(heroMap.get(record.getSup2Id()));
        });
        return recordList;
    }

    /**
     * 新增游戏对局记录
     *
     * @param lolGameRecord 游戏对局记录
     * @return 结果
     */
    @Override
    public int insertLolGameRecord(LolGameRecord lolGameRecord) {
        return lolGameRecordMapper.insertLolGameRecord(lolGameRecord);
    }

    /**
     * 修改游戏对局记录
     *
     * @param lolGameRecord 游戏对局记录
     * @return 结果
     */
    @Override
    public int updateLolGameRecord(LolGameRecord lolGameRecord) {
        return lolGameRecordMapper.updateLolGameRecord(lolGameRecord);
    }

    /**
     * 批量删除游戏对局记录
     *
     * @param ids 需要删除的游戏对局记录主键
     * @return 结果
     */
    @Override
    public int deleteLolGameRecordByIds(Long[] ids) {
        return lolGameRecordMapper.deleteLolGameRecordByIds(ids);
    }

    /**
     * 删除游戏对局记录信息
     *
     * @param id 游戏对局记录主键
     * @return 结果
     */
    @Override
    public int deleteLolGameRecordById(Long id) {
        return lolGameRecordMapper.deleteLolGameRecordById(id);
    }

    @Override
    public AjaxResult importData(List<LolGameRecord> gameRecordList) {
        List<LolHero> lolHeroList = lolHeroMapper.selectLolHeroList(new LolHero());
        Date gameTime = null;
        for (LolGameRecord gameRecord : gameRecordList) {
            if (gameRecord.getGameTime() != null) {
                gameTime = gameRecord.getGameTime();
            } else if (gameTime != null) {
                gameRecord.setGameTime(gameTime);
            }
            gameRecord.setTopId(findHeroId(lolHeroList, gameRecord.getTop()));
            gameRecord.setJungleId(findHeroId(lolHeroList, gameRecord.getJungle()));
            gameRecord.setMidId(findHeroId(lolHeroList, gameRecord.getMid()));
            gameRecord.setAdId(findHeroId(lolHeroList, gameRecord.getAd()));
            gameRecord.setSupId(findHeroId(lolHeroList, gameRecord.getSup()));
            gameRecord.setTop2Id(findHeroId(lolHeroList, gameRecord.getTop2()));
            gameRecord.setJungle2Id(findHeroId(lolHeroList, gameRecord.getJungle2()));
            gameRecord.setMid2Id(findHeroId(lolHeroList, gameRecord.getMid2()));
            gameRecord.setAd2Id(findHeroId(lolHeroList, gameRecord.getAd2()));
            gameRecord.setSup2Id(findHeroId(lolHeroList, gameRecord.getSup2()));
        }
        lolGameRecordMapper.insertLolGameRecordList(gameRecordList);
        return AjaxResult.success();
    }

    public static Long findHeroId(List<LolHero> lolHeroList, String pattern) {
        Pattern p = Pattern.compile(pattern);
        for (LolHero hero : lolHeroList) {
            if (p.matcher(hero.getName()).find()) {
                return hero.getId();
            } else if (p.matcher(hero.getTitle()).find()) {
                return hero.getId();
            } else if (hero.getNickName()!=null&&p.matcher(hero.getNickName()).find()) {
                return hero.getId();
            }
        }
        throw new ServiceException("未找到对应英雄：" + pattern);
    }
}
